﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Windows.Forms;
using DC2010.Objects.Data;
using DC2010.Tools;
using DC2010;
using System.Reflection;
using EventInfo = DC2010.Objects.Data.EventInfo;

namespace DC2010_EditorInfo
{
    public partial class EventsForm : Form
    {
        public EventInfo EventInfo { get; set; }
        public string OriginalId { get; set; }


        public EventsForm()
        {
            InitializeComponent();

            EventInfo = new EventInfo();
        }

        private void EffectsForm_Load(object sender, EventArgs e)
        {
            InitializeLB();
            InitializeEffectLogicCombo();
        }

        private void InitializeLB()
        {
            imageListBox1.Items.Clear();

            foreach (EventInfo ei in Root.I.EventSystem.DictInfo.Values)
            {

                ImageListBoxItem ilbi = new ImageListBoxItem(ei.Id, Properties.Resources.Effect);
                imageListBox1.Items.Add(ilbi);
            }
        }

        private void InitializeEffectLogicCombo()
        {
            MethodsCB.Items.Clear();

            try
            {
                Assembly ass = Assembly.LoadFile(Path.Combine(Environment.CurrentDirectory,"DC2010_Core.dll"));

                if (ass != null)
                {
                    Type[] t = ass.GetTypes();
                    List<string> lst = new List<string>();
                    foreach (Type type in t)
                    {
                        if ((type.BaseType!=null)&&(type.BaseType.Name == "EventLogic"))
                        {
                            //we have winner
                            lst.Add(type.Name);
                            Root.log.InfoFormat("Editor, Adding event: {0}", type.Name);
                        }
                    }

                    List<string> x = (from i in lst orderby i select i).ToList();
                    MethodsCB.Items.AddRange(x.ToArray());
                }
            }
            catch (Exception ex)
            {
                Root.log.ErrorFormat("Editor, Error initializing combo with event methods. {0}", ex);
            }

        }

        private void crystalButton1_Click(object sender, EventArgs e)
        {
            if (!string.IsNullOrEmpty(OriginalId))
            {
                //we are updating
                GatherInfo(EventInfo);
                Root.I.EventSystem.SaveInfo();
                InitializeLB();
            }
            else
            {
                //creating new
                GatherInfo(EventInfo);
                Root.I.EventSystem.AddInfo(EventInfo);
                Root.I.EventSystem.SaveInfo();
                InitializeLB();
            }
        }

        private void GatherInfo(EventInfo ei)
        {
            ei.Id = IdTB.Text;
            ei.ShortName = ShortTB.Text;
            ei.LongName = DescriptionTB.Text;
            ei.DivinePoints = DivinePointsTB.Text.ToInt();
            ei.Method = MethodsCB.Text;
            ei.Effects = keyValueUC1.Data;
            ei.Conditions = keyValueUC2.Data;
            ei.Params = keyValueUC3.Data;

        }

        private void FillWithObject()
        {
            if (EventInfo != null)
            {
                OriginalId = EventInfo.Id;
                IdTB.Text = EventInfo.Id;
                ShortTB.Text = EventInfo.ShortName;
                DescriptionTB.Text = EventInfo.LongName;
                DivinePointsTB.Text = EventInfo.DivinePoints.ToString();
                MethodsCB.Text = EventInfo.Method;
                keyValueUC1.FillList();
                keyValueUC2.FillList();
                keyValueUC3.FillList();

            }
        }

        private void imageListBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            string s = ((ImageListBoxItem)imageListBox1.SelectedItem).Text;
            EventInfo = Root.I.EventSystem.GetInfo(s);
            FillWithObject();
        }

        private void crystalButton2_Click(object sender, EventArgs e)
        {
            EventInfo = new EventInfo();
            OriginalId = "";
        }
    }
}
